package com.tongzhan.service;

import com.tongzhan.base.BaseCriteria;
import com.tongzhan.entity.MarkPrompt;
import com.tongzhan.entity.User;
import com.tongzhan.repository.MarkPromptRepository;
import com.tongzhan.repository.UserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @author htmic
 * @date 2020/7/31
 */
@Service
public class MarkPromptService {

    @Autowired
    private MarkPromptRepository markPromptRepository;

    @Autowired
    private UserRepository userRepository;

    /**
     * 添加
     *
     * @param markPrompt
     */
    public void add(MarkPrompt markPrompt) {
        new Thread(() -> {
            //查询需要提示的用户
            List<User> userList = userRepository.findAll(new Query(BaseCriteria.where("isDelete").is(0).and("type").is(3)));
            if (!CollectionUtils.isEmpty(userList)) {
                List<MarkPrompt> list = new ArrayList<>(userList.size());
                userList.forEach(item->{
                    MarkPrompt entity = new MarkPrompt();
                    BeanUtils.copyProperties(markPrompt, entity);
                    entity.setMarkFlag(0);
                    entity.setType(1);
                    entity.setIsDelete(0);
                    entity.setAddTime(new Date());
                    list.add(entity);
                });
                markPromptRepository.saveAll(list);
            }
        }).start();
    }

    /**
     * 添加
     * @param markPrompt
     * @param duties 角色
     */
    public void add(MarkPrompt markPrompt, String ...duties) {
        new Thread(() -> {
            //查询需要提示的用户
            List<User> userList = userRepository.findAll(new Query(BaseCriteria.where("isDelete").is(0).and("type").is(1).and("dutiesCode").in(duties)));
            if (!CollectionUtils.isEmpty(userList)) {
                List<MarkPrompt> list = new ArrayList<>(userList.size());
                userList.forEach(item->{
                    MarkPrompt entity = new MarkPrompt();
                    BeanUtils.copyProperties(markPrompt, entity);
                    entity.setUserId(item.getUserId());
                    entity.setMarkFlag(0);
                    entity.setType(1);
                    entity.setIsDelete(0);
                    entity.setAddTime(new Date());
                    list.add(entity);
                });
                markPromptRepository.saveAll(list);
            }
        }).start();
    }

    /**
     * 获取列表
     *
     * @return
     */
    public Map<String, Object> getList(String userId) {
        //党派(1:活动中心)
        long actionMarkCount = markPromptRepository.count(new Query(BaseCriteria.where("isDelete").is(0).and("type").is(1).and("markType").is(1).and("markFlag").is(0).and("userId").is(userId)));
        //党派(2：参政议政)
        long participationCount = markPromptRepository.count(new Query(BaseCriteria.where("isDelete").is(0).and("type").is(1).and("markType").is(2).and("markFlag").is(0).and("userId").is(userId)));
        //党派(3:工作安排)
        long workCount = markPromptRepository.count(new Query(BaseCriteria.where("isDelete").is(0).and("type").is(1).and("markType").is(3).and("markFlag").is(0).and("userId").is(userId)));
        //党派(4:社情民意)
        long opinionCount = markPromptRepository.count(new Query(BaseCriteria.where("isDelete").is(0).and("type").is(1).and("markType").is(4).and("markFlag").is(0).and("userId").is(userId)));
        //党派(5:通知中心)
        long noticeCount = markPromptRepository.count(new Query(BaseCriteria.where("isDelete").is(0).and("type").is(1).and("markType").is(5).and("markFlag").is(0).and("userId").is(userId)));
        //党派(6：发展新员)
        long newPersonCount = markPromptRepository.count(new Query(BaseCriteria.where("isDelete").is(0).and("type").is(1).and("markType").is(6).and("markFlag").is(0).and("userId").is(userId)));

        Map<String, Object> result = new HashMap<>(3);
        result.put("actionMarkCount", actionMarkCount);
        result.put("participationCount", participationCount);
        result.put("workCount", workCount);
        result.put("opinionCount", opinionCount);
        result.put("noticeCount", noticeCount);
        result.put("newPersonCount", newPersonCount);

        return result;
    }

    /**
     * 标记已读
     * @param markId
     */
    public void read(String markId, String userId){
        MarkPrompt mark = markPromptRepository.findOne(
                new Query(BaseCriteria.where("isDelete").is(0).and("type").is(1).and("markId").is(markId).and("userId").is(userId)));
        if(mark!=null){
            mark.setMarkFlag(1);
            markPromptRepository.updateByPrimaryKeySelective(mark);
        }
    }
}
